package hfuu.com.cn.teasystemapp;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import hfuu.com.cn.teasystemapp.bean.operate;
import hfuu.com.cn.teasystemapp.bean.operatesq;
import hfuu.com.cn.teasystemapp.util.HttpUtils;
import hfuu.com.cn.teasystemapp.util.listviewutil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by git on 2018/5/24.
 */

public class operatesqacivity extends Activity {

    List<operate> operates;
    SimpleAdapter adapter;
    private List<HashMap<String, Object>> operatesqdata;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.operate);
        operatesqdata=new ArrayList<HashMap<String, Object>>();
        Intent old=getIntent();
        operates=new ArrayList<operate>();
        operates=(List<operate>) old.getSerializableExtra("operatelist");
        Log.i("operates",operates.toString());

        for (operate operate:
             operates) {
            HashMap<String, Object> item = new HashMap<String, Object>();
            item.put("operateid",operate.getOperateId());
            item.put("teaxid",operate.getWarehouseTeaxid());
            item.put("teaname",operate.getWarehouseteax().getWarehouseTeaname());
            item.put("total",operate.getWarehouseteaxlog().getWarehouseTeaxquality());
            item.put("isfocus", false);
            operatesqdata.add(item);
        }
        ImageView back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ListView list= (ListView) findViewById(R.id.operatelist);
        adapter=new SimpleAdapter(this,operatesqdata,R.layout.operatelistitem,new String[]{"teaname","total"},new int[]{R.id.teaname,R.id.total}){
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                Boolean isFocus = (Boolean) operatesqdata.get(position).get("isfocus");

                if (isFocus) {
                    view.setBackgroundColor(getResources().getColor(R.color.tv_gray));
                } else {
                    view.setBackgroundColor(Color.WHITE);
                }
                return view;
            }
        };
        list.setAdapter(adapter);
        listviewutil.setListViewHeightBasedOnChildren(list);


        FloatingActionButton fb= (FloatingActionButton) findViewById(R.id.sq);
        fb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Log.i("landland",landList.toString());
                HttpUtils.doGet("sq/listad", new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String res=response.body().string();Intent i=new Intent();
                        Log.i("sssssssssssssssssss",res);
                        if(res.length()<=0){
                            i.putExtra("sqlist","");
                        }else {
                            Gson gson = new Gson();
                            List<operatesq> sqlist = new ArrayList<operatesq>();
                            sqlist = (ArrayList<operatesq>) gson.fromJson(res, new TypeToken<List<operatesq>>() {
                            }.getType());

                            i.putExtra("sqlist", (Serializable) sqlist);
                        }
                        i.setClass(operatesqacivity.this, sqloglist.class);
                        startActivity(i);
                    }
                });

            }
        });
        /**
         * 加工处理
         */
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                operatesqdata.get(position).put("isfocus", true);
                for (int i = 0; i < operatesqdata.size(); i++) {
                    if (i != position) {
                        operatesqdata.get(i).put("isfocus", false);
                        adapter.getView(i, view, parent);
                    }

                }
                adapter.getView(position, view, parent);
                adapter.notifyDataSetChanged();
                String operateid= (String) operatesqdata.get(position).get("operateid");
                String teaxid=(String)operatesqdata.get(position).get("teaxid");
                Intent i=new Intent();
                i.setClass(operatesqacivity.this,sqadd.class);
                i.putExtra("id",operateid);
                i.putExtra("teaxid",teaxid);
                startActivity(i);
            }
        });


    }
}
